package com.project.common.utils.number;

import com.project.common.utils.StringUtils;
import lombok.extern.log4j.Log4j2;

import java.math.BigDecimal;

/**
 * @Remark: 数字处理工具类
 * @Author: 汪焰
 * @date: 2020/8/24 16:09
 */
@Log4j2
public class NumberUtils extends org.apache.commons.lang3.math.NumberUtils {

    public static boolean isEntity(Number num) {
        if (null == num || compare(num, 0)) {
            return true;
        }
        return false;
    }

    public static boolean isNotEntity(Number num) {
        return !isEntity(num);
    }

    public static boolean isNull(Number num) {
        if (null == num) {
            return true;
        }
        return false;
    }

    public static boolean isNotNull(Number num) {
        return !isNull(num);
    }

    public static boolean compare(Number num1, Number num2) {
        if (null == num1 || null == num2) {
            return false;
        }
        Double num11 = num1.doubleValue();
        Double num22 = num2.doubleValue();
        return num11.compareTo(num22) == 0;
    }

    /**
     * @Remark: num1 > num2 返回 true
     * @Remark: 大于
     * @Author: 汪焰
     * @date: 2020/8/25 16:37
     */
    public static boolean gt(Number num1, Number num2) {
        try {
            if (null == num1 || null == num2) {
                return false;
            }
            BigDecimal one = new BigDecimal(String.valueOf(num1));
            BigDecimal two = new BigDecimal(String.valueOf(num2));
            return one.compareTo(two) == 1;
        } catch (Exception e) {
            log.error("num1 与 num2 比较大小异常 num1:{},num1:{}", num1, num2);
        }
        return false;
    }

    /**
     * @Remark: 相反数 例如：10的相反数 -10
     * @Author: 汪焰
     * @date: 2020/8/25 18:01
     */
    public static Double opposite(Number num) {
        BigDecimal zero = new BigDecimal(0);
        BigDecimal numB = new BigDecimal(String.valueOf(num));
        return zero.subtract(numB).doubleValue();
    }

    public static Long convertToLong(Integer num) {
        if (isNull(num)) {
            return null;
        }
        Long result = Long.valueOf(String.valueOf(num));
        return result;
    }

    public static Long convertToLong(String num) {
        if (StringUtils.isBlank(num)) {
            return null;
        }
        Long result = Long.valueOf(num);
        return result;
    }

    public static String toStr(Number num) {
        if (isNull(num)) {
            return StringUtils.EMPTY;
        }
        return String.valueOf(num);
    }

    public static int size(Number num) {
        if (num == null) {
            return 0;
        }
        return String.valueOf(num).length();
    }
}
